/* $Id: Layout.java 137 2011-08-29 20:33:36Z altdotua@gmail.com $ */

package alt.djudge.frontend.client.ui.guest;

import alt.djudge.frontend.client.rpc.DJudgeGWTPublicService;
import alt.djudge.frontend.client.rpc.DJudgeGWTPublicServiceAsync;
import alt.djudge.frontend.client.ui.ContentPage;
import alt.djudge.frontend.shared.dto.contest.ContestInfoDTO;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.logical.shared.ValueChangeEvent;
import com.google.gwt.event.logical.shared.ValueChangeHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Anchor;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.ScrollPanel;
import com.google.gwt.user.client.ui.VerticalPanel;


public class Layout extends Composite
{
	public static DJudgeGWTPublicServiceAsync publicService = GWT.create(DJudgeGWTPublicService.class);
	
	interface MyUiBinder extends UiBinder<DockLayoutPanel, Layout> {}

	private static MyUiBinder uiBinder = GWT.create(MyUiBinder.class);

	private static Layout layout;
	
	public static Layout getLayout()
	{
		return layout;
	}
	
	@UiField
	public ScrollPanel contentPanel;

	ContentPage[] pages;

	@UiField
	public FlowPanel flowPanel;
	
	public Layout()
	{
		initWidget(uiBinder.createAndBindUi(this));

		layout = this;
		
		pages = new ContentPage[flowPanel.getWidgetCount()];
		for (int i = 0; i < pages.length; i++)
			pages[i] = (ContentPage) flowPanel.getWidget(i);
		
		History.addValueChangeHandler(new ValueChangeHandler<String>()
		{
			public void onValueChange(ValueChangeEvent<String> event)
			{
				String historyToken = event.getValue();
				onHistoryEvent(historyToken);
			}
		});
		
		onHistoryEvent(History.getToken());
		
		publicService.getContests(-1, new AsyncCallback<ContestInfoDTO[]>()
		{
			
			@Override
			public void onSuccess(ContestInfoDTO[] result)
			{
				VerticalPanel panel = new VerticalPanel();
				for (ContestInfoDTO entry : result)
				{
					//String html = entry.getFieldValue(2);
					//String href = "/contest.html#id/" + entry.getId().toString();
					//Anchor anchor = new Anchor(html, href);
					//panel.add(anchor);
				}
				flowPanel.add(new HTML("<h1>List of contests</h1>"));
				flowPanel.add(panel);
			}
			
			@Override
			public void onFailure(Throwable caught)
			{
				Window.alert(caught.toString());
			}
		});
	}
	
	public void onHistoryEvent(String historyToken)
	{
		for (ContentPage page : pages)
		{
			page.setVisible(false);
		}

		int cnt = 0;
		for (ContentPage page : pages)
		{
			cnt++;
			String pageToken = page.getHistoryTokenPrefix();
			if (cnt == pages.length || historyToken.startsWith(pageToken))
			{
				page.setVisible(true);
				String restToken = pageToken.length() + 1 <= historyToken.length() ? historyToken.substring(pageToken.length() + 1) : "";
				page.dispatch(restToken);
				return;
			}
		}
	}
}
